//
//  SACollectionViewLayout.m
//  SunOn
//
//  Created by mc on 2017/5/3.
//  Copyright © 2017年 NeoZhang. All rights reserved.
//  信息收集页面layout

#import "SACollectionViewLayout.h"


@interface SACollectionViewLayout(){
    NSInteger _cellCount;//cell数量
    CGPoint _center;//中心点
    CGFloat _radius;//宽高比
}
@end

@implementation SACollectionViewLayout

- (void)prepareLayout {
    [super prepareLayout];
    CGSize size = self.collectionView.frame.size;
    _cellCount = [[self collectionView] numberOfItemsInSection:0];
    _center = CGPointMake(size.width / 2.0, size.height / 2.0);
    _radius = MIN(size.width, size.height) / 2.5;
}


//整个collectionView的内容大小就是collectionView的大小（没有滚动）
-(CGSize)collectionViewContentSize
{
    return CGSizeMake(SASCREEN_WIDTH,SASCREEN_HEIGHT - 108);
}

- (nullable UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewLayoutAttributes* attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; //生成空白的attributes对象，其中只记录了类型是cell以及对应的位置是indexPath
    if (indexPath.row == 0) {
        CGSize size = CGSizeMake(SASCREEN_WIDTH, 125);
        CGRect frame = CGRectMake(0, 0, size.width, size.height);
        attributes.frame = frame;
        return attributes;
    }else if (indexPath.row < 6) {
        CGSize size = CGSizeMake(SASCREEN_WIDTH / 5, 100);
        CGFloat y = 125;
        CGFloat x = 0;
        CGRect frame = CGRectMake(x + (indexPath.row - 1) * size.width, y, size.width, size.height);
        attributes.frame = frame;
        return attributes;
    }else if (indexPath.row == 6) {
        CGSize size = CGSizeMake(SASCREEN_WIDTH, 40);
        CGRect frame = CGRectMake(0, 235, size.width, size.height);
        attributes.frame = frame;
        return attributes;
    }else {
        attributes.frame = CGRectMake(0, 276, SASCREEN_WIDTH, SASCREEN_HEIGHT - 276 - 108);
        return attributes;
    }
}

//用来在一开始给出一套UICollectionViewLayoutAttributes
-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSMutableArray* attributes = [NSMutableArray array];
    for (NSInteger i=0 ; i < _cellCount; i++) {
        //这里利用了-layoutAttributesForItemAtIndexPath:来获取attributes
        NSIndexPath* indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        [attributes addObject:[self layoutAttributesForItemAtIndexPath:indexPath]];
    }
    return attributes;
}

@end
